<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-wms__task}">
      <el-form ref="dataForm" :model="operateForm" :rules="dataRule" label-width="80px">
        <el-row :gutter="20">
          <el-col :md="5" :sm="5">
            <el-form-item label="批次">
              <el-input ref="inputLotNo" v-model="operateForm.lotNo" @keyup.enter.native="getSpuByLot"
                placeholder="请输入批次" maxlength="32" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="6">
            <el-form-item label="砂芯图号" prop="spuNo">
              <el-select v-model="operateForm.spuNo" @change="pickSpuNo" placeholder="请选择上架砂芯">
                <el-option v-for="item in options" :key="item.spuId" :label="item.spuNo" :value="item">
                  <span style="float: left">{{ item.spuNo }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.lotNo }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="7" :sm="7">
            <el-form-item label="托盘条码" prop="contNo">
              <el-input @keyup.enter.native="manualRegister" ref="inputContNo" v-model="operateForm.contNo"
                placeholder="请输入托盘条码" maxlength="32" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="6">
            <el-form-item>
              <el-button type="primary" @click="manualRegister()">{{ $t("affirm") }}
              </el-button>
              <el-button type="danger" @click="manualGo()">放行</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" highlight-current-row border
        @selection-change="dataListSelectionChangeHandle" style="width: 100%">
        <el-table-column prop="taskId" label="id" header-align="center" align="center" sortable></el-table-column>
        <el-table-column prop="taskType" label="任务类型" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.taskType === '01'" type="info">上架</el-tag>
            <el-tag v-else>未知</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="taskSt" label="任务状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.taskSt === 0" type="warning">已扫码</el-tag>
            <el-tag v-else-if="scope.row.taskSt === 1" type="success">已放行</el-tag>
            <el-tag v-else-if="scope.row.taskSt === 2" type="success">已完成</el-tag>
            <el-tag v-else type="danger">异常</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="originContNo" label="托盘条码" header-align="center" align="center" min-width="220"
          show-overflow-tooltip></el-table-column>
        <el-table-column prop="spuNo" label="砂芯图号" header-align="center" align="center" min-width="135">
        </el-table-column>
        <el-table-column prop="lotNo" label="批次" header-align="center" align="center">
        </el-table-column>
        <el-table-column prop="planQnty" label="计划量" header-align="center" align="center"></el-table-column>
        <el-table-column prop="actlQnty" label="实际量" header-align="center" align="center"></el-table-column>
        <el-table-column prop="originPosition" label="起始位置" header-align="center" align="center" min-width="150">
        </el-table-column>
        <el-table-column prop="targetPosition" label="目标位置" header-align="center" align="center" min-width="150">
        </el-table-column>
        <el-table-column prop="currentPosition" label="当前位置" header-align="center" align="center" min-width="150">
        </el-table-column>
        <el-table-column prop="stockId" label="库存id" header-align="center" align="center"></el-table-column>

        <el-table-column prop="creatorName" label="创建用户" header-align="center" align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="createDate" label="创建日期" header-align="center" align="center" min-width="155" sortable>
        </el-table-column>
        <el-table-column prop="updaterName" label="更新用户" header-align="center" align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="updateDate" label="更新日期" header-align="center" align="center" min-width="155" sortable>
        </el-table-column>
        <el-table-column prop="comments" label="备注" header-align="center" align="center" show-overflow-tooltip>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
        layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/wms/task/page",
        getDataListIsPage: true,
        deleteIsBatch: false,
      },
      dataForm: {
        taskId: '',
        taskType: '01',
        taskSubType: '013',
      },
      operateForm: {
        contNo: '',
        spuNo: '',
        lotNo: ''
      },
      options: []
    };
  },
  created() {
    this.getSpuInfo();
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.inputLotNo.focus();
    })
  },
  computed: {
    dataRule() {
      return {
        spuNo: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        contNo: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
      };
    },
  },
  methods: {
    //1.获取砂芯图号
    getSpuInfo() {
      this.$http.get(`/wms/spu-info/list`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data;
      }).catch(() => { })
    },
    //2.选中砂芯
    pickSpuNo(value) {
      this.operateForm.spuNo = value.spuNo;
      this.operateForm.lotNo = value.lotNo;
    },
    //3.根据批号匹配砂芯
    getSpuByLot() {
      this.operateForm.spuNo = '';
      this.options.forEach(item => {
        if (item.lotNo == this.operateForm.lotNo) {
          this.operateForm.spuNo = item.spuNo;
          this.$nextTick(() => {
            this.$refs.inputContNo.focus();
            this.$refs.inputContNo.select();
          })
          return;
        }
      });
    },
    //4.人工组盘
    manualRegister() {
      this.$refs["dataForm"].validate((valid) => {
        if (!valid) {
          return false;
        }
        this.$http.post(`/wms/bind/register`, {
          "contNo": this.operateForm.contNo,
          "spuNo": this.operateForm.spuNo
        }).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: res.msg,
            type: "success",
            duration: 500,
            onClose: () => {
              this.query();
              this.$nextTick(() => {
                this.$refs.inputContNo.focus();
                this.$refs.inputContNo.select();
              })
            },
          });
        }).catch(() => { });
      })
    },
    //5.模拟放行
    manualGo() {
      this.$http.post(`/wms/simulate/go`, {
        "positionNo": "2F_NOTICE_ST24",
        "amount": "2"
      }).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.$message({
          message: res.msg,
          type: "success",
          duration: 500,
          onClose: () => {
            this.query();
            this.$nextTick(() => {
              this.$refs.inputContNo.focus();
              this.$refs.inputContNo.select();
            })
          },
        });
      }).catch(() => { });
    }
  },
};
</script>
